home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
- From: Dan Pop <danpop@mail.cern.ch>
- Newsgroups: comp.lang.c
- Subject: Re: What's wrong here?
- Date: Wed, 31 Jan 1996 13:09:39 +0100
- Organization: CERN European Lab for Particle Physics
- Message-ID: <9601311209.AA06949@dxmint.cern.ch>
- References: <4eml5o$o6h@airdmhor.gen.nz>
- X-NNTP-Posting-Host: hpl3sn03.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
- X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
-
- gumboot@airdmhor.gen.nz (Simon Hosie) writes:
-
- > 1: #include <stdio.h>
- > 2:
- > 3: typedef unsigned short word;
- > 4:
- > 5: int main(void)
- > 6: {
- > 7: word a, b, c, d;
- > 8:
- > 9: a = 1;
- > 10: b = 2;
- > 11: c = 4;
- > 12: d = a | b | c;
- > 13:
- > 14: d |= a;
- > 15:
- > 16: d |= a | b;
- > 17: d = d | a | b;
- > 18:
- > 19: return 0;
- > 20: }
- >
- > Watcom gives the following warnings (and _only_ those warnings) compiling
- >the above code (except without the line numbers or colons), but only when
- >it's compiling it as C++ and generating 32 bit code (GCC doesn't, no matter
- >how hard I try). I don't know C++ myself, but my flatmate is learning it,
- >does it have anything to do with the typedef?
-
- When you suspect something like this, the easiest thing is to remove the
- typedef and see if this changes anything.
-
- >TEST.CPP(12): Warning! W389: (col 15) integral value may be truncated during
- > assignment or initialization
- >TEST.CPP(17): Warning! W389: (col 15) integral value may be truncated during
- > assignment or initialization
-
- There's nothing wrong with the code, the compiler is plain silly (and you
- can complain to your vendor).
-
- Here's what happens at line 12: the type of the expression a | b | c is
- int (not unsigned short, because of the integral promotions) while the
- type of d is unsigned short, which in your particular case is a shorter
- type. The compiler notices that it assigns a 32-bit int to a 16-bit
- unsigned short and produces the stupid warning, despite the fact that the
- result of the expression is guaranteed to fit into 16 bits.
-
- But the most blatant stupidity of the compiler is that it warned at line
- 17, but it silently accepted line 16, which is identical to 17 for all
- intents and purposes. Both "=" and "|=" are assignment operators, but the
- compiler seems to treat them differently when emitting stupid warnings.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-